package org.example.testrabbitmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;

import java.util.concurrent.TimeoutException;

/**
 * direct类型的生产者
 */
public class TestProducer {
    public final static String EXCHANGE_NAME = "direct_logs";
    public static void main(String[] args)throws IOException, TimeoutException {
      //创建连接到rabbitmq
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.56.103");
        factory.setVirtualHost("testRabbit");
        factory.setUsername("anlu");
        factory.setPassword("anlu");
        Connection connection = factory.newConnection();


        //创建信道
        Channel channel = connection.createChannel();
        // 创建交换器
        channel.exchangeDeclare(EXCHANGE_NAME,"direct");
        /*日志消息级别，作为路由键使用*/
        String[] serverities = {"error","info","warning"};
        for(int i=0;i<3;i++){
            String severity = serverities[i%3];
            String msg = "Hellol,RabbitMq"+(i+1);
            /*发布消息，需要参数：交换器，路由键，其中以日志消息级别为路由键*/
            channel.basicPublish(EXCHANGE_NAME,severity,null,
                    msg.getBytes());
            System.out.println("Sent "+severity+":"+msg);
        }
        channel.close();
        connection.close();

    }
}
